package com.songcha.library_base.compose

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.colorResource
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.library_base.interfaces.IRefreshData
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.songcha.library_base.R
import com.songcha.library_base.mvvm.base.BaseRefreshViewModel
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_compose_refresh.NestedScrollMode
import com.songcha.library_compose_refresh.UltraSwipeRefresh
import com.songcha.library_compose_refresh.indicator.SwipeRefreshHeader
import com.songcha.library_compose_refresh.indicator.classic.ClassicRefreshFooter
import com.songcha.library_compose_refresh.rememberUltraSwipeRefreshState

abstract class BaseComposeRefreshMvvmFragment<VM: BaseRefreshViewModel<out BaseRepository, out Any>>:BaseComposeMvvmFragment<VM>() ,
    IRefreshData {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    @Composable
    protected open fun RefreshView(content:@Composable () -> Unit){
        val srState = rememberUltraSwipeRefreshState()
        val refresh_state by viewModel.refresh_state.observeAsState()
        srState.isRefreshing = refresh_state==BaseRefreshViewModel.RefreshStateVM.REFRESHING

        UltraSwipeRefresh(
            state = srState,
            onRefresh = {
                viewModel.onRefreshing()
                onGetData()
            },
            onLoadMore = {

            },
            modifier = Modifier.background(color = colorResource(id = com.songcha.library_common.R.color.white)),
            loadMoreEnabled = true,
            headerScrollMode = NestedScrollMode.Translate,
            footerScrollMode = NestedScrollMode.Translate,
            headerIndicator = {
                SwipeRefreshHeader(it, contentColor = colorResource(id = com.songcha.library_common.R.color.primary))
            },
            footerIndicator = {
                ClassicRefreshFooter(it)
            }
        ){
            content()
        }
    }


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        viewModel.refresh_state.observe(this){
            when(it){
                BaseRefreshViewModel.RefreshStateVM.REFRESHING->onRefreshing()
                BaseRefreshViewModel.RefreshStateVM.REFRESH_SUCCESS->onRefreshSuccess()
                BaseRefreshViewModel.RefreshStateVM.REFRESH_ERROR->onRefreshError(viewModel.refresh_error!!)
                BaseRefreshViewModel.RefreshStateVM.REFRESH_NO_NETWORK->onRefreshNoNetwork()
                BaseRefreshViewModel.RefreshStateVM.REFRESH_CONNECT_ERROR->onRefreshConnectError()
                BaseRefreshViewModel.RefreshStateVM.REFRESH_NO_DATA->onRefreshNoData()
                BaseRefreshViewModel.RefreshStateVM.REFRESH_TIMEOUT->onRefreshTimeOut()
            }
        }

        addRlstObserve()

    }

    protected open fun addRlstObserve(){
        viewModel!!.rlst.observe(this){
            if(it!=null && it.size>0){
                //initListData()
            }
        }
    }

    //abstract fun initListData()

    //abstract fun getRefreshLayout(): SmartRefreshLayout

    //abstract fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>?

    override fun onRefreshing() {

    }

    override fun onRefreshSuccess() {
        //srl?.finishRefresh(true)
        //getListAdapter()?.notifyDataSetChanged()
    }

    override fun onRefreshError(e: Throwable) {
        //srl?.finishRefresh(false)
    }

    override fun onRefreshNoData() {
        //srl?.finishRefresh(false)
    }

    override fun onRefreshNoNetwork() {
        //srl?.finishRefresh(false)
    }

    override fun onRefreshConnectError() {
        //srl?.finishRefresh(false)
    }

    override fun onRefreshTimeOut() {
        //srl?.finishRefresh(false)
    }
}